package com.martian.common.util;

import org.json.JSONArray;
import org.json.JSONObject;

import static com.martian.common.util.HttpClient.asyncSinglePostRequest;

/**
 * @author martian
 * @version V1.0.0
 * @Description
 * @date 2023-05-17 17:44
 */

public class ModelServing {
    public static JSONArray predict(JSONArray instances,String model){
        JSONObject instancesRoot = new JSONObject();
        instancesRoot.put("instances", instances);
        String predictionScores = null;
        switch (model){
            case "NeuralCF":
                predictionScores = asyncSinglePostRequest("http://47.113.180.77:8503/v1/models/NeuralCF:predict", instancesRoot.toString());
                break;
            case "DeepCrossing":
                predictionScores = asyncSinglePostRequest("http://47.113.180.77:8502/v1/models/DeepCrossing:predict", instancesRoot.toString());
                break;
            case "WideAndDeep":
                predictionScores = asyncSinglePostRequest("http://47.113.180.77:8501/v1/models/WideAndDeep:predict", instancesRoot.toString());
                break;
        }
        JSONObject predictionsObject = new JSONObject(predictionScores);
        JSONArray scores = predictionsObject.getJSONArray("predictions");
        return scores;
    }
}
